#encoding:utf-8
import sys
sys.path.append('..')
import libs.util.http_request_ssl as httpReq
import threading
import time
import config

GLOBAL_ACCESS_TOKEN = 9

def get_token():
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s'%(config.app_id, config.app_secret)
    http_req = httpReq(url)
    return http_req.do_get()

class timer(threading.Thread): #The timer class is derived from the class threading.Thread
    def __init__(self, num, interval):
        threading.Thread.__init__(self)  
        self.thread_num = num  
        self.interval = interval
        self.thread_stop = False  
   
    def run(self): #Overwrite run() method, put what you want the thread do here  
        while not self.thread_stop:
            t = get_token()
            t = eval( t ) #字符串转换成dict类型
            global GLOBAL_ACCESS_TOKEN
            GLOBAL_ACCESS_TOKEN = t['access_token']

            time.sleep(self.interval)
    def stop(self):  
        self.thread_stop = True  


thread1 = timer(1, 5600)
thread1.start()


def get_access_token():   #返回的值
    GLOBAL_ACCESS_TOKEN
    return GLOBAL_ACCESS_TOKEN


if __name__ == '__main__':
    print get_access_token()

